package com.swh.user.controller;

import java.util.HashMap;
import java.util.Map;

import com.swh.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import com.swh.user.entity.TbUser;
import com.swh.user.service.TbUserService;

import com.swh.base.entity.Result;
import com.swh.base.entity.StatusCode;

import javax.servlet.http.HttpServletRequest;

/**
 * @author swh
 * @Description 用户 控制器层
 * @date 2021-01-15 17:27:10
 */
@RestController
@CrossOrigin
@Api(value = "tbUser")
@RequestMapping("/tbUser")
public class TbUserController {

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    HttpServletRequest request;


    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result register(@RequestBody TbUser user) {
        TbUser tbUser = tbUserService.login(user.getMobile(), user.getPassword());

        if (tbUser != null) {

            String token = JwtUtil.createJWT(user.getMobile(), user.getPassword(), "user");
            Map map = new HashMap();
            map.put("token",token);
            map.put("nickname",user.getNickname());
            map.put("avatar",user.getAvatar());

            return new Result(StatusCode.OK, "登录成功",map);
        }
        return new Result(StatusCode.USER_PASSWORD_ERROR, "用户名或密码错误");
    }


    /**
     * 用户注册
     *
     * @return
     **/
    @RequestMapping(value = "/register/{code}", method = RequestMethod.POST)
    public Result register(@RequestBody TbUser user, @PathVariable("code") String code) {
        Result register = tbUserService.register(user, code);
        return register;
    }

    /**
     * 发送验证码
     *
     * @return
     **/
    @RequestMapping(value = "/sendsms/{mobile}", method = RequestMethod.POST)
    public Result sendsms(@PathVariable("mobile") String mobile) {
        tbUserService.sendsms(mobile);
        return new Result(StatusCode.OK, "发送成功");
    }

    /**
     * 查询全部数据
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(StatusCode.OK, "查询成功", tbUserService.findAll());
    }

    /**
     * 根据ID查询
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        return new Result(StatusCode.OK, "查询成功", tbUserService.findById(id));
    }

    /**
     * 分页+多条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<TbUser> pageList = tbUserService.findSearch(searchMap, page, size);
        return new Result(StatusCode.OK, "查询成功", pageList);
    }

    /**
     * 根据条件查询
     *
     * @param searchMap
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result findSearch(@RequestParam Map searchMap) {
        return new Result(StatusCode.OK, "查询成功", tbUserService.findSearch(searchMap));
    }

    /**
     * 增加
     *
     * @param tbUser
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(TbUser tbUser) {
        tbUserService.add(tbUser);
        return new Result(StatusCode.OK, "增加成功");
    }

    /**
     * 修改
     *
     * @param tbUser
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(TbUser tbUser, @PathVariable String id) {
        tbUser.setId(id);
        tbUserService.update(tbUser);
        return new Result(StatusCode.OK, "修改成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {

        Claims claims = (Claims) request.getAttribute("user_auth");
        if (claims == null) {
            return new Result(StatusCode.ACCESS_ERROR, "无权访问");
        }

        tbUserService.deleteById(id);
        return new Result(StatusCode.OK, "删除成功");
    }

}
